package com.common.app.data.repository

import com.common.app.data.api.ApiConstant.getBaseUrl
import com.common.app.data.bean.AppConfigBean
import com.common.app.data.bean.AdInfoBean
import com.common.app.data.bean.VersionResBean
import com.common.base.data.CommonBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import rxhttp.toAwait
import rxhttp.wrapper.param.RxHttp

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/19
 * <p>
 * 描述：公共通用
 * <p>
 * 修订历史：
 */
object CommonPurposeApi {

    //获取验证码
     val COMMON_PUR_VER_CODE = getBaseUrl() + "api/v1/smart-ring/send-code"

    //检测版本更新
    private val COMMON_PUR_CHECK_APP_VER = getBaseUrl() + "versions/maxVersion"

    //获取html链接前半部分等配置信息
    private val USER_GET_APP_CONFIG = getBaseUrl() + "api/v1/common/h5/blink"

    //获取开屏图信息
    val USER_GET_SPLASH_PIC_LIST = getBaseUrl() + "api/v1/ad/{type}"

    //省市区三级联动
    val COMMON_PUR_PST_ADDRESS = getBaseUrl() + "region/getByParent"

    //渠道跳入数据上报
    val UP_DISTR_INFO = getBaseUrl() + "api/v1/distr/log"

    //获取验证码:3打包密码2忘记密码4登录1注册7修改手机5注销账户8恢复账号6公益捐款9兑换鸡蛋10老兵(用户)回归11实名认证
    suspend fun requestVerCode(phone:String, type:Int, countryCode:String?) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(COMMON_PUR_VER_CODE)
                .add("Mobile",phone)
                .toAwait<CommonBean<Any>>()
                .await()
        }

    //APP配置
    suspend fun getAppConfig() =
        withContext(Dispatchers.IO) {
            RxHttp.get(USER_GET_APP_CONFIG)
//                .connectTimeout(3000)
                .toAwait<CommonBean<AppConfigBean>>()
                .await()
        }

    //检测版本更新
    suspend fun requestVersion() =
        withContext(Dispatchers.IO) {
            RxHttp.get("${COMMON_PUR_CHECK_APP_VER}/0?appId=2")
                .toAwait<CommonBean<VersionResBean>>()
                .await()
        }

    //获取开屏图信息
    suspend fun requestSplashPicList(type : String?="") =
        withContext(Dispatchers.IO) {
            RxHttp.get(USER_GET_SPLASH_PIC_LIST)
                .addPath("type",type)
                .toAwait<CommonBean<AdInfoBean>>()
                .await()
        }
    //省市区三级联动
    suspend fun requestLevelAddress(mCode:String?) =
        withContext(Dispatchers.IO) {
            RxHttp.get(COMMON_PUR_PST_ADDRESS)
                .addQuery(mCode)
                .toAwait<CommonBean<AdInfoBean>>()
                .await()
        }

    suspend fun uploadDistr(distrId: String, routeUrl: String?) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(UP_DISTR_INFO)
                .add("DistrId",distrId)//分销Id
                .add("RouteUrl",routeUrl)//页面路由
                .toAwait<CommonBean<Any>>()
                .await()
        }
}